Java POI读取excel中小数位数多了,丢失精度,自动补0的问题 |
您所在的位置:网站首页 › excel 位数不够补0 › Java POI读取excel中小数位数多了,丢失精度,自动补0的问题 |
话不多说直接上图,有两位小数,一位小数的,和只有整数的,我这列单元格格式是常规。 然后在Java里面读取的时候 1 --> 1.0 很好,为了解决问题,用网上的办法: cell.setCellType(CellType.STRING); //读取前将单元格设置为文本类型读取 结果到1.09时,读取出来是这样的 1.09 --> 1.09000000000000001 然后肯定插入数据的时候会报错啥的 目前的解决办法: 一. 将excel单元格改为文本类型(我用的这种方法解决了问题) 注意,直接修改单元格属性不管用, 使用 分列 的方式,Excel分列不会的可以百度下,可以实现将数值改为文本类型。 二. java处理(暂时还没试过,先记录下来) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class CommonUtil {
private static NumberFormat numberFormat = NumberFormat.getNumberInstance();
static { numberFormat.setGroupingUsed(false); }
public static String getCellValue(Cell cell) { if (null == cell) { return ""; } Object value; switch (cell.getCellTypeEnum()) { // 省略 case NUMERIC: double d = cell.getNumericCellValue(); value = numberFormat.format(d); // 关键在这里! //省略 } return value == null ? "" : value.toString(); } }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |